<?php

namespace app\api\controller;
use think\Controller;
use think\Db;

/**
 * swagger: 文章
 */
class Article extends Base
{
	/**
	 * get: 文章列表
	 * path: list
	 * method: list
	 * param: rows - {int} 当前页
	 * param: pagesize - {int} 每页显示数据
	 */
	public function list($rows, $pagesize)
    {
        if($rows != '' || $pagesize != ''){	
			$info = Db::name('article')->order('id desc')->limit($rows . ',' . $pagesize)->field('title,photo,create_time')->select();
			if ($info) {
				$data['code'] = 200;
				$data['datas'] = $info;
				$data['msg'] = '获取文章列表成功';
				return json($data);	
			} else {
				$data['code'] = 404;
				$data['msg'] = '获取文章列表失败';
				return json($data);	
			}		
		}else{
			$data['code'] = 400;
			$data['msg'] = '参数错误';
			return json($data);
		}
    }

	//获取文章列表
	public function article_list()
	{
		$page = input("page", 1, "intval");
		$cid = input("cid", 2, "intval");

		$list = db("news")->where(['cate_id' => $cid, 'status' => ['neq', '0']])->page($page, 10)->cache(60)->order("status desc,create_time desc")->select();
		$list = $this->array_image_add_domain($list, 'photo');
		$list = $this->array_time_change($list, 'create_time', 'Y-m-d');

		$this->success('获取成功', $list);
	}

	//获取文章
	public function article()
	{
		$id = input("id", 1, "intval");

		$result = db("news")->where(["id" => $id, "status" => ['neq', '0']])->find();

		if ($result) {
			db("news")->where("id", $id)->setInc("views");

			$result['photo'] = $this->array_image_add_domain(explode(',', $result['photo']), '');

			$result['create_time'] = date("Y-m-d", $result['create_time']);

			$this->success("查询成功", ['result' => $result]);
		} else {
			$this->error("信息不存在");
		}
	}
}